@page "/docio/update-fields"
@inject Microsoft.JSInterop.IJSRuntime JS
@using System.IO
@using Syncfusion.Blazor.Buttons
@using BlazorDemos.Data.FileFormats.DocIO
@inject  Microsoft.AspNetCore.Hosting.IWebHostEnvironment hostingEnvironment;
@inherits SampleBaseComponent;

<SampleDescription>
    <p style="font-size:16px">This sample demonstrates how to update the fields available in the Word document using Essential DocIO. Here, in the example, merge fields and formula fields are used together to populate the net profit or loss.</p>
</SampleDescription>
<ActionDescription>
    <p style="font-size:16px">Fields in a Word document are like placeholders where the data can be populated or calculated dynamically based on various factors. Most commonly used fields are merge fields, Table of contents (TOC), page number related fields, IF fields, date fields etc.</p>&nbsp;
    <p style="font-size:16px">Another example: IF field compares two values and then inserts the text appropriate to the rest of the comparison. When used in a mail merge, the field can examine information in the merged data records, and populate appropriate text.</p>&nbsp;
    <p style="font-size:16px">More information about the fields and updating fields can be found in this <a href="https://help.syncfusion.com/file-formats/docio/working-with-fields">documentation</a> section.</p>
</ActionDescription>

    <div class="control-section">
        <p style="font-size:16px">Click the button to view the generated Word document. Please note that Microsoft Word Viewer or Microsoft Word is required to view the resultant Word document.</p>
        <table>
            <tr>
                <td>
                    <h5 class="row" style="font-size:16px">Save As:</h5>
                </td>
                <td>
                    <div class="col-xs-12 col-sm-12 col-lg-1 col-md-6">
                        <SfRadioButton Label="DOC" Name="DOC" Value=@("DOC") @bind-Checked="@documentType"></SfRadioButton>
                    </div>
                </td>
                <td>
                    <div class="col-xs-12 col-sm-12 col-lg-1 col-md-6">
                        <SfRadioButton Label="DOCX" Name="DOCX" Value=@("DOCX") @bind-Checked="@documentType"></SfRadioButton>
                    </div>
                </td>
                <td>
                    <div class="col-xs-12 col-sm-12 col-lg-1 col-md-6">
                        <SfRadioButton Label="WordML" Name="WordML" Value=@("WordML") @bind-Checked="@documentType"></SfRadioButton>
                    </div>
                </td>
            </tr>
            <tr>
                <td colspan="4">
                    <div class="button-section">
                        <div id="button-control">
                            <div class="row">
                                <div>
                                    <SfButton @onclick="ViewTemplate">View Template</SfButton>&nbsp;
                                    <SfButton @onclick="GenerateDocument">Generate Document</SfButton>
                                </div>
                            </div>
                        </div>
                    </div>
                </td>
            </tr>
        </table>
    </div>

<style>
    .control-section .row {
        margin: 10px 0;
    }
</style>

@code {
    MemoryStream documentStream;
    string documentType = "DOCX";
    /// <summary>
    /// Update fields and download the Word document
    /// </summary>
    public async Task GenerateDocument(MouseEventArgs args)
    {
        UpdateFieldsService service = new UpdateFieldsService(hostingEnvironment);
        documentStream = service.UpdateFields(documentType, "Generate Document");
        if (documentType == "DOC")
            await JS.SaveAs("Update Fields.doc", documentStream.ToArray());        
        else if(documentType == "WordML")
            await JS.SaveAs("Update Fields.xml", documentStream.ToArray());       
        else
            await JS.SaveAs("Update Fields.docx", documentStream.ToArray());        
    }
    /// <summary>
    /// Download the template Word document
    /// </summary>
    public async Task ViewTemplate(MouseEventArgs args)
    {
        UpdateFieldsService service = new UpdateFieldsService(hostingEnvironment);
        documentStream = service.UpdateFields(documentType, "View Template");
        await JS.SaveAs("TemplateUpdateFields.docx", documentStream.ToArray());        
    }
}